package com.wyw.hemerocallis.partiition;

import com.alibaba.fastjson.JSON;
import com.wyw.hemerocallis.vo.WywMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy;
import org.springframework.cloud.stream.binder.PartitionSelectorStrategy;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * 决定 message 发送到哪个分区的策略
 *
 * @author Mr Wu    yewen.wu.china@gmail.com
 * <p>
 * Update History:
 * Author        Time            Content
 */
@Slf4j
@Component
public class WywPartitionSelectorStrategy implements PartitionSelectorStrategy {

    /**
     * 选择分区策略
     * @param key
     * @param partitionCount
     * @return
     */
    @Override
    public int selectPartition(Object key, int partitionCount) {
        int partition = key.toString().hashCode()%partitionCount;
        log.info("SpringCloud Stream Wyw Selector info: [{}], [{}], [{}]",
                key.toString(), partitionCount, partition);

        return partition;
    }
}
